//
//  LGOAuthViewController.swift
//  01-Weibo项目练习-1
//
//  Created by 李李刚 on 16/3/12.
//  Copyright © 2016年 李李刚. All rights reserved.
//

import UIKit

// appkey
private let WB_APPKEY = "418721235"
// 点击授权之后跳转的页面
private let WB_REDIRECT_URI = "http://www.itheima.com/"

//页面要加载微博的登录界面,所以view使用webView

class LGOAuthViewController: UIViewController {
    
    //懒加载一个webView
    private lazy var webView: UIWebView = {
       
       let webView = UIWebView ()
        
        return webView
        
    }()
    
    //重写loadView方法,在里面设置view为webView
    override func loadView() {
        
        view = webView
        
        //创建网络请求
        let regiest = NSURLRequest(URL: OAuthLoginUrl)
        
        webView.loadRequest(regiest)
        
    }
    
    
    private lazy var OAuthLoginUrl = NSURL(string: "https://api.weibo.com/oauth2/authorize?client_id=\(WB_APPKEY)&redirect_uri=\(WB_REDIRECT_URI)")!
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()

    }


    private func setupUI() {
        
        view.backgroundColor = UIColor.whiteColor()
        
        //左侧返回按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: nil, title: "返回", target: self, action: "backDidClick")
        
        //右侧自动填充按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: nil, title: "自动填充", target: self, action: "autoFill")
        
    }
    
    //返回按钮点击事件
    @objc private func backDidClick() {
        
        dismissViewControllerAnimated(true, completion: nil)
        
        
    }
    
    //自动填充按钮点击事件
    @objc private func autoFill() {
        
        print("自动填充")
        
        let jsString = "document.getElementById('userId').value='vgfjh15964@163.com';document.getElementById('passwd').value='qw1987'"
        // 让当前 webView执行以上这句js代码
        webView.stringByEvaluatingJavaScriptFromString(jsString)
        
    }
    
    
    
    


}
